#include <stdio.h>
#include <string.h>
#include "quick_sort.h"


struct Student
{
    int sno;
    char name[21];
    char sex;  // 0 表示女，1 表示男
    float sight;
};

//调用者根据需要排序的数据的实际情况自己定义比较函数比较函数
int cmp_int(const void* a, const void* b)
{
    return *(int*)a - *(int*)b;
}


int cmp_str(const void* a, const void* b)
{
    return strcmp(*(char**)b, *(char**)a);
}


// 一级排序：视力不好的优先
// 二级排序：女士优先
// 三级排序：按学号降序
int cmp_stu(const void* a, const void* b)
{
    struct Student* s1 = (struct Student*)a, *s2 = (struct Student*)b;

    if(s1->sight > s2->sight)
        return 1;
    else if(s1->sight == s2->sight)
    {
        if(s1->sex == s2->sex)
        {
            return s2->sno - s1->sno;
        }
        
        return s1->sex - s2->sex;
    }
    else 
        return -1;
}


int main()
{
    int nums[] = {5, 3, 4, 1, 2};

    char* names[] = {"abc", "a12", "ab", "Abcde", "1"};

    struct Student stus[] = 
    {
        {1001, "张三", 0, 3.9}, 
        {1002, "李四", 1, 3.9}, 
        {1003, "王五", 0, 3.9}, 
        {1004, "赵六", 1, 3.7}, 
        {1005, "田七", 0, 4.2}, 
    };

    quick_sort(nums, 5, sizeof(int), cmp_int);
    
    quick_sort(names, 5, sizeof(char*), cmp_str);

    quick_sort(stus, 5, sizeof(struct Student), cmp_stu);

    int i;
    for(i = 0; i < 5; i++)
    {
        printf("%d ", nums[i]);
    }

    printf("\n");

    for(i = 0; i < 5; i++)
    {
        printf("%s ", names[i]);
    }

    printf("\n");    

    for(i = 0; i < 5; i++)
    {
        printf("%d %s %d %g\n", stus[i].sno, stus[i].name, stus[i].sex, stus[i].sight);
    }

    printf("\n"); 

    return 0;
}
